<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>QQ风格聊天输入框</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f7fb;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .chat-container {
            width: 100%;
            max-width: 500px;
            background-color: #ffffff;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            display: flex;
            flex-direction: column;
            height: 700px;
        }

        .chat-header {
            padding: 16px 20px;
            background-color: #4a6fa5;
            color: white;
            font-size: 1.2em;
            font-weight: 600;
            text-align: center;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .chat-header img {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            object-fit: cover;
        }

        .chat-messages {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
            background-color: #f9fafa;
            display: flex;
            flex-direction: column;
            gap: 16px;
        }

        /* 消息容器 - 使用flex布局，左侧或右侧对齐 */
        .message-container {
            display: flex;
            gap: 12px;
            max-width: 80%;
        }

        /* 自己的消息右对齐 */
        .message-container.self {
            align-self: flex-end;
            flex-direction: row-reverse;
            gap: 12px;
        }

        /* 头像样式 */
        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 8px;
            object-fit: cover;
            flex-shrink: 0;
            border: 1px solid #e0e0e0;
        }

        /* 消息内容框 */
        .message-box {
            display: flex;
            flex-direction: column;
            gap: 4px;
        }

        /* 消息气泡 */
        .message {
            padding: 12px 16px;
            border-radius: 18px;
            line-height: 1.5;
            font-size: 15px;
            position: relative;
            animation: fadeIn 0.3s ease-out;
            word-wrap: break-word;
            max-width: 100%;
        }

        /* 自己的消息 - 蓝色，右对齐 */
        .message.self {
            background-color: #4a6fa5;
            color: white;
            border-bottom-right-radius: 4px;
            align-self: flex-end;
        }

        /* 别人的消息 - 灰色，左对齐 */
        .message.other {
            background-color: #e5e5ea;
            color: #333;
            border-bottom-left-radius: 4px;
            align-self: flex-start;
        }

        /* 发送者信息 */
        .sender-info {
            display: flex;
            align-items: center;
            gap: 6px;
            font-size: 12px;
            color: #666;
            min-height: 16px;
        }

        .sender-name {
            font-weight: 600;
            color: #333;
        }

        .send-time {
            color: #999;
        }

        /* 图片消息样式 */
        .message-image {
            max-width: 200px;
            max-height: 200px;
            border-radius: 12px;
            margin-top: 4px;
            cursor: pointer;
            border: 2px solid #4a6fa5;
        }

        /* 文件消息样式 */
        .message-file {
            display: flex;
            align-items: center;
            background-color: #f0f0f0;
            padding: 10px;
            border-radius: 8px;
            margin-top: 4px;
            cursor: pointer;
        }

        .file-icon {
            width: 36px;
            height: 36px;
            background-color: #4a6fa5;
            border-radius: 8px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            margin-right: 10px;
        }

        .file-info {
            flex: 1;
        }

        .file-name {
            font-size: 14px;
            font-weight: 500;
            color: #333;
        }

        .file-size {
            font-size: 12px;
            color: #666;
        }

        .input-area {
            display: flex;
            flex-direction: column;
            padding: 12px 16px;
            background-color: #ffffff;
            border-top: 1px solid #e0e0e0;
            gap: 10px;
        }

        .tools-bar {
            display: flex;
            gap: 12px;
            padding: 0 4px;
            opacity: 0.8;
            transition: opacity 0.2s;
        }

        .tools-bar:hover {
            opacity: 1;
        }

        .tool-btn {
            background: none;
            border: none;
            cursor: pointer;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #555;
            transition: all 0.2s ease;
        }

        .tool-btn:hover {
            background-color: #f0f0f0;
            transform: scale(1.1);
        }

        .tool-btn svg {
            width: 20px;
            height: 20px;
        }

        .input-box-wrapper {
            display: flex;
            align-items: end;
            gap: 10px;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 24px;
            transition: all 0.3s ease;
        }

        .input-box-wrapper:focus-within {
            border-color: #4a6fa5;
            box-shadow: 0 0 0 3px rgba(74, 111, 165, 0.2);
        }

        .input-box {
            flex: 1;
            border: none;
            outline: none;
            resize: none;
            max-height: 120px;
            min-height: 24px;
            line-height: 1.5;
            font-size: 15px;
            padding: 4px 0;
            background: transparent;
        }

        .input-box::placeholder {
            color: #aaa;
        }

        .send-btn {
            width: 40px;
            height: 40px;
            border: none;
            background-color: #4a6fa5;
            color: white;
            border-radius: 50%;
            cursor: pointer;
            display: flex;
            justify-content: center;
            align-items: center;
            transition: all 0.2s ease;
            flex-shrink: 0;
        }

        .send-btn:hover {
            background-color: #3a5a80;
            transform: scale(1.05);
        }

        .send-btn:active {
            transform: scale(0.98);
        }

        .send-btn svg {
            width: 20px;
            height: 20px;
            fill: white;
        }

        .send-btn.disabled {
            background-color: #ccc;
            cursor: not-allowed;
            transform: none;
        }

        .emoji-picker {
            position: absolute;
            bottom: 80px;
            right: 20px;
            background: white;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            padding: 8px;
            display: none;
            z-index: 10;
            max-width: 250px;
            max-height: 300px;
            overflow: auto;
        }

        .emoji-picker.show {
            display: block;
        }

        .emoji-category {
            display: flex;
            flex-wrap: wrap;
            gap: 6px;
            padding: 4px;
        }

        .emoji-item {
            font-size: 20px;
            cursor: pointer;
            padding: 4px;
            border-radius: 6px;
            transition: transform 0.1s;
        }

        .emoji-item:hover {
            transform: scale(1.2);
            background-color: #f5f5f5;
        }

        /* 消息淡入动画 */
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* 图片查看器 */
        .image-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.9);
            justify-content: center;
            align-items: center;
            z-index: 1000;
        }

        .image-modal.show {
            display: flex;
        }

        .modal-content {
            max-width: 90%;
            max-height: 90%;
        }

        .modal-content img {
            max-width: 100%;
            max-height: 90vh;
            border-radius: 8px;
        }

        /* 隐藏文件输入 */
        #fileInput, #imageInput {
            display: none;
        }

        /* 响应式设计 */
        @media (max-width: 480px) {
            .chat-container {
                margin: 10px;
                border-radius: 12px;
                height: 90vh;
            }
            .input-box {
                font-size: 14px;
            }
            .message {
                padding: 10px 14px;
            }
            .avatar {
                width: 36px;
                height: 36px;
            }
        }
    </style>
</head>
<body>
<div class="chat-container">
    <!-- 聊天标题 -->
    <div class="chat-header">
        <img src="https://i.pravatar.cc/150?img=12" alt="头像">
        <span>群聊</span>
    </div>

    <!-- 消息显示区域 -->
    <div class="chat-messages" id="messages">
        <!-- 示例消息 - 别人发送 -->
        <div class="message-container other">
            <img class="avatar" src="https://i.pravatar.cc/150?img=5" alt="头像">
            <div class="message-box">
                <div class="sender-info">
                    <span class="sender-name">小明</span>
                    <span class="send-time">10:05</span>
                </div>
                <div class="message other">你好！欢迎加入聊天室！</div>
            </div>
        </div>

        <!-- 示例消息 - 自己发送 -->
        <div class="message-container self">
            <img class="avatar" src="https://i.pravatar.cc/150?img=12" alt="头像">
            <div class="message-box">
                <div class="sender-info" style="justify-content: flex-end;">
                    <span class="send-time">10:06</span>
                    <span class="sender-name">龙茶清欢(我)</span>
                </div>
                <div class="message self">谢谢！很高兴认识大家！</div>
            </div>
        </div>
    </div>

    <!-- 工具栏 -->
    <div class="tools-bar">
        <button class="tool-btn" id="emojiBtn" title="表情">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
                <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm-5-9c.75 0 1.41.4 1.75 1H6c.34-.6.75-1 1.5-1zm8 0c-.75 0-1.41.4-1.75 1h3.5c-.34-.6-.75-1-1.5-1zm-5.5 5c0 1.1.9 2 2 2s2-.9 2-2-.9-2-2-2-2 .9-2 2z" fill="#555"/>
            </svg>
        </button>
        <button class="tool-btn" id="imageBtn" title="发送图片">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
                <path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V8L14,2Z" fill="#555"/>
            </svg>
        </button>
        <button class="tool-btn" id="fileBtn" title="发送文件">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
                <path d="M14,2H6A2,2 0 0,0 4,4V20A2,2 0 0,0 6,22H18A2,2 0 0,0 20,20V8L14,2M18,20H6V4H13V9H18V20Z" fill="#555"/>
            </svg>
        </button>
    </div>

    <!-- 输入区域 -->
    <div class="input-area">
        <!-- 表情选择器 -->
        <div class="emoji-picker" id="emojiPicker">
            <div class="emoji-category">
                <span class="emoji-item" data-emoji="😀">😀</span>
                <span class="emoji-item" data-emoji="😁">😁</span>
                <span class="emoji-item" data-emoji="😂">😂</span>
                <span class="emoji-item" data-emoji="🤣">🤣</span>
                <span class="emoji-item" data-emoji="😃">😃</span>
                <span class="emoji-item" data-emoji="😄">😄</span>
                <span class="emoji-item" data-emoji="😅">😅</span>
                <span class="emoji-item" data-emoji="😆">😆</span>
                <span class="emoji-item" data-emoji="😉">😉</span>
                <span class="emoji-item" data-emoji="😊">😊</span>
                <span class="emoji-item" data-emoji="😋">😋</span>
                <span class="emoji-item" data-emoji="😎">😎</span>
                <span class="emoji-item" data-emoji="😍">😍</span>
                <span class="emoji-item" data-emoji="😘">😘</span>
                <span class="emoji-item" data-emoji="🥰">🥰</span>
                <span class="emoji-item" data-emoji="😗">😗</span>
                <span class="emoji-item" data-emoji="😙">😙</span>
                <span class="emoji-item" data-emoji="😚">😚</span>
                <span class="emoji-item" data-emoji="🙂">🙂</span>
                <span class="emoji-item" data-emoji="🤗">🤗</span>
                <span class="emoji-item" data-emoji="🤩">🤩</span>
                <span class="emoji-item" data-emoji="🤔">🤔</span>
                <span class="emoji-item" data-emoji="🤨">🤨</span>
                <span class="emoji-item" data-emoji="😐">😐</span>
                <span class="emoji-item" data-emoji="😑">😑</span>
                <span class="emoji-item" data-emoji="😶">😶</span>
                <span class="emoji-item" data-emoji="🙄">🙄</span>
                <span class="emoji-item" data-emoji="😏">😏</span>
                <span class="emoji-item" data-emoji="😣">😣</span>
                <span class="emoji-item" data-emoji="😥">😥</span>
                <span class="emoji-item" data-emoji="😮">😮</span>
                <span class="emoji-item" data-emoji="🤐">🤐</span>
                <span class="emoji-item" data-emoji="😯">😯</span>
                <span class="emoji-item" data-emoji="😪">😪</span>
                <span class="emoji-item" data-emoji="😫">😫</span>
                <span class="emoji-item" data-emoji="🥱">🥱</span>
                <span class="emoji-item" data-emoji="😴">😴</span>
                <span class="emoji-item" data-emoji="😌">😌</span>
                <span class="emoji-item" data-emoji="😛">😛</span>
                <span class="emoji-item" data-emoji="😜">😜</span>
                <span class="emoji-item" data-emoji="😝">😝</span>
                <span class="emoji-item" data-emoji="🤤">🤤</span>
                <span class="emoji-item" data-emoji="😒">😒</span>
                <span class="emoji-item" data-emoji="😓">😓</span>
                <span class="emoji-item" data-emoji="😔">😔</span>
                <span class="emoji-item" data-emoji="😕">😕</span>
                <span class="emoji-item" data-emoji="🙃">🙃</span>
                <span class="emoji-item" data-emoji="🤑">🤑</span>
                <span class="emoji-item" data-emoji="😲">😲</span>
                <span class="emoji-item" data-emoji="☹️">☹️</span>
                <span class="emoji-item" data-emoji="🙁">🙁</span>
                <span class="emoji-item" data-emoji="😖">😖</span>
                <span class="emoji-item" data-emoji="😞">😞</span>
                <span class="emoji-item" data-emoji="😟">😟</span>
                <span class="emoji-item" data-emoji="😤">😤</span>
                <span class="emoji-item" data-emoji="😢">😢</span>
                <span class="emoji-item" data-emoji="😭">😭</span>
                <span class="emoji-item" data-emoji="😦">😦</span>
                <span class="emoji-item" data-emoji="😧">😧</span>
                <span class="emoji-item" data-emoji="😨">😨</span>
                <span class="emoji-item" data-emoji="😩">😩</span>
                <span class="emoji-item" data-emoji="🤯">🤯</span>
                <span class="emoji-item" data-emoji="😬">😬</span>
                <span class="emoji-item" data-emoji="😰">😰</span>
                <span class="emoji-item" data-emoji="😱">😱</span>
                <span class="emoji-item" data-emoji="🥵">🥵</span>
                <span class="emoji-item" data-emoji="🥶">🥶</span>
                <span class="emoji-item" data-emoji="😳">😳</span>
                <span class="emoji-item" data-emoji="🤪">🤪</span>
                <span class="emoji-item" data-emoji="😵">😵</span>
                <span class="emoji-item" data-emoji="🥴">🥴</span>
                <span class="emoji-item" data-emoji="😠">😠</span>
                <span class="emoji-item" data-emoji="😡">😡</span>
                <span class="emoji-item" data-emoji="🤬">🤬</span>
                <span class="emoji-item" data-emoji="😷">😷</span>
                <span class="emoji-item" data-emoji="🤒">🤒</span>
                <span class="emoji-item" data-emoji="🤕">🤕</span>
                <span class="emoji-item" data-emoji="🤢">🤢</span>
                <span class="emoji-item" data-emoji="🤮">🤮</span>
                <span class="emoji-item" data-emoji="🤧">🤧</span>
                <span class="emoji-item" data-emoji="😇">😇</span>
                <span class="emoji-item" data-emoji="🥳">🥳</span>
                <span class="emoji-item" data-emoji="🥺">🥺</span>
                <span class="emoji-item" data-emoji="🤠">🤠</span>
                <span class="emoji-item" data-emoji="🤡">🤡</span>
                <span class="emoji-item" data-emoji="🤥">🤥</span>
                <span class="emoji-item" data-emoji="🤫">🤫</span>
                <span class="emoji-item" data-emoji="🤭">🤭</span>
                <span class="emoji-item" data-emoji="🤔">🤔</span>
                <span class="emoji-item" data-emoji="🤭">🤭</span>
                <span class="emoji-item" data-emoji="🤐">🤐</span>
                <span class="emoji-item" data-emoji="🤗">🤗</span>
            </div>
        </div>

        <!-- 文件输入 -->
        <input type="file" id="imageInput" accept="image/*">
        <input type="file" id="fileInput" accept="*">

        <!-- 输入框 -->
        <div class="input-box-wrapper">
        <textarea
                class="input-box"
                id="messageInput"
                placeholder="输入消息..."
                maxlength="500"
        ></textarea>
            <button class="send-btn" id="sendBtn">
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
                    <path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z" fill="white"/>
                </svg>
            </button>
        </div>
    </div>

    <!-- 图片查看器 -->
    <div class="image-modal" id="imageModal">
        <div class="modal-content">
            <img id="modalImage" src="" alt="查看大图">
        </div>
    </div>
</div>

<script>
    // 获取DOM元素
    const messageInput = document.getElementById('messageInput');
    const sendButton = document.getElementById('sendBtn');
    const messagesContainer = document.getElementById('messages');
    const emojiBtn = document.getElementById('emojiBtn');
    const emojiPicker = document.getElementById('emojiPicker');
    const imageBtn = document.getElementById('imageBtn');
    const fileBtn = document.getElementById('fileBtn');
    const imageInput = document.getElementById('imageInput');
    const fileInput = document.getElementById('fileInput');
    const imageModal = document.getElementById('imageModal');
    const modalImage = document.getElementById('modalImage');

    // 当前用户信息
    const currentUser = {
        name: '我',
        avatar: 'https://i.pravatar.cc/150?img=12',
        role: 'user'
    };

    // 其他用户信息
    const otherUser = {
        name: '小明',
        avatar: 'https://i.pravatar.cc/150?img=5',
        role: 'user'
    };

    // 获取当前时间（格式：HH:MM）
    function getCurrentTime() {
        const now = new Date();
        return now.getHours().toString().padStart(2, '0') + ':' +
            now.getMinutes().toString().padStart(2, '0') + ':' +
            now.getSeconds().toString().padStart(2, '0');
    }

    // 判断输入框是否为空，控制发送按钮状态
    function updateSendButton() {
        const hasText = messageInput.value.trim().length > 0;
        if (hasText) {
            sendButton.classList.remove('disabled');
        } else {
            sendButton.classList.add('disabled');
        }
    }

    // 创建消息元素
    function createMessageElement(content, isSelf = true, type = 'text', fileData = null) {
        // 创建消息容器
        const messageContainer = document.createElement('div');
        messageContainer.className = `message-container ${isSelf ? 'self' : 'other'}`;

        // 创建头像
        const avatar = document.createElement('img');
        avatar.className = 'avatar';
        avatar.src = isSelf ? currentUser.avatar : otherUser.avatar;
        avatar.alt = '头像';

        // 创建消息盒子
        const messageBox = document.createElement('div');
        messageBox.className = 'message-box';

        // 创建发送者信息行
        const senderInfo = document.createElement('div');
        senderInfo.className = 'sender-info';

        // 显示昵称和时间
        const nameSpan = document.createElement('span');
        nameSpan.className = 'sender-name';
        nameSpan.textContent = isSelf ? currentUser.name : otherUser.name;

        const timeSpan = document.createElement('span');
        timeSpan.className = 'send-time';
        timeSpan.textContent = getCurrentTime();

        // 根据消息方向调整昵称和时间的顺序
        if (isSelf) {
            senderInfo.appendChild(timeSpan);
        } else {
            senderInfo.appendChild(nameSpan);
            senderInfo.appendChild(timeSpan);
        }

        // 创建消息内容
        let messageContent;
        if (type === 'text') {
            messageContent = document.createElement('div');
            messageContent.className = `message ${isSelf ? 'self' : 'other'}`;
            messageContent.textContent = content;
        } else if (type === 'image') {
            messageContent = document.createElement('div');
            messageContent.className = `message ${isSelf ? 'self' : 'other'}`;

            const img = document.createElement('img');
            img.className = 'message-image';
            img.src = fileData;
            img.alt = content;

            // 点击图片查看大图
            img.addEventListener('click', () => {
                modalImage.src = fileData;
                imageModal.classList.add('show');
            });

            messageContent.appendChild(document.createTextNode(content || ''));
            if (content) messageContent.appendChild(document.createElement('br'));
            messageContent.appendChild(img);
        } else if (type === 'file') {
            messageContent = document.createElement('div');
            messageContent.className = `message ${isSelf ? 'self' : 'other'}`;

            const fileContainer = document.createElement('div');
            fileContainer.className = 'message-file';

            const fileIcon = document.createElement('div');
            fileIcon.className = 'file-icon';
            fileIcon.innerHTML = '📁';

            const fileInfo = document.createElement('div');
            fileInfo.className = 'file-info';

            const fileName = document.createElement('div');
            fileName.className = 'file-name';
            fileName.textContent = content;

            const fileSize = document.createElement('div');
            fileSize.className = 'file-size';
            fileSize.textContent = formatFileSize(fileData.size);

            fileInfo.appendChild(fileName);
            fileInfo.appendChild(fileSize);
            fileContainer.appendChild(fileIcon);
            fileContainer.appendChild(fileInfo);

            messageContent.appendChild(fileContainer);
        }

        // 添加到消息盒子
        messageBox.appendChild(senderInfo);
        messageBox.appendChild(messageContent);

        // 添加到容器
        messageContainer.appendChild(avatar);
        messageContainer.appendChild(messageBox);

        return messageContainer;
    }

    // 格式化文件大小
    function formatFileSize(bytes) {
        if (bytes === 0) return '0 Bytes';
        const k = 1024;
        const sizes = ['Bytes', 'KB', 'MB', 'GB'];
        const i = Math.floor(Math.log(bytes) / Math.log(k));
        return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    }

    // 发送消息函数
    function sendMessage() {
        const text = messageInput.value.trim();
        if (!text) return; // 空消息不发送

        // 创建新的消息元素
        const messageElement = createMessageElement(text, true, 'text');

        // 添加到消息容器
        messagesContainer.appendChild(messageElement);

        // 清空输入框并更新按钮状态
        messageInput.value = '';
        updateSendButton();

        // 滚动到底部
        scrollToBottom();

        // 可以在这里添加发送到服务器的逻辑
        console.log('发送消息:', text);
    }

    // 发送图片
    function sendImage(file) {
        const reader = new FileReader();
        reader.onload = function(e) {
            const messageElement = createMessageElement(
                messageInput.value.trim(),
                true,
                'image',
                e.target.result
            );

            messagesContainer.appendChild(messageElement);
            scrollToBottom();

            // 清空输入框
            messageInput.value = '';
            updateSendButton();
        };
        reader.readAsDataURL(file);
    }

    // 发送文件
    function sendFile(file) {
        const messageElement = createMessageElement(
            file.name,
            true,
            'file',
            file
        );

        messagesContainer.appendChild(messageElement);
        scrollToBottom();

        // 清空输入框
        messageInput.value = '';
        updateSendButton();
    }

    // 滚动到底部
    function scrollToBottom() {
        messagesContainer.scrollTop = messagesContainer.scrollHeight;
    }

    // 监听输入事件，实时更新发送按钮状态
    messageInput.addEventListener('input', updateSendButton);

    // 监听键盘事件
    messageInput.addEventListener('keydown', function(e) {
        // 如果按下Enter且没有按住Shift，则发送消息
        if (e.key === 'Enter' && !e.shiftKey) {
            e.preventDefault(); // 阻止默认换行
            sendMessage();
        }
    });

    // 点击发送按钮发送消息
    sendButton.addEventListener('click', function() {
        if (!sendButton.classList.contains('disabled')) {
            sendMessage();
        }
    });

    // 自动调整输入框高度
    messageInput.addEventListener('input', function() {
        this.style.height = 'auto'; // 重置高度
        // 滚动高度可能会超过最大值，需要限制
        const maxHeight = 120;
        const scrollHeight = this.scrollHeight;
        this.style.height = scrollHeight > maxHeight ? `${maxHeight}px` : `${scrollHeight}px`;
    });

    // 表情按钮点击事件
    emojiBtn.addEventListener('click', function(e) {
        e.stopPropagation();
        emojiPicker.classList.toggle('show');
    });

    // 点击表情
    document.querySelectorAll('.emoji-item').forEach(item => {
        item.addEventListener('click', function() {
            const emoji = this.getAttribute('data-emoji');
            messageInput.value += emoji;
            updateSendButton();
            messageInput.focus();

            // 自动调整高度
            messageInput.style.height = 'auto';
            messageInput.style.height = messageInput.scrollHeight > 120 ? '120px' : `${messageInput.scrollHeight}px`;
        });
    });

    // 点击页面其他地方隐藏表情选择器
    document.addEventListener('click', function(e) {
        if (!emojiPicker.contains(e.target) && e.target !== emojiBtn) {
            emojiPicker.classList.remove('show');
        }
    });

    // 图片按钮点击
    imageBtn.addEventListener('click', function() {
        imageInput.click();
    });

    // 文件按钮点击
    fileBtn.addEventListener('click', function() {
        fileInput.click();
    });

    // 图片选择处理
    imageInput.addEventListener('change', function() {
        if (this.files && this.files[0]) {
            const file = this.files[0];
            // 检查是否为图片
            if (file.type.startsWith('image/')) {
                sendImage(file);
            } else {
                alert('请选择有效的图片文件');
            }
            // 重置输入值，以便可以选择同一文件
            this.value = '';
        }
    });

    // 文件选择处理
    fileInput.addEventListener('change', function() {
        if (this.files && this.files[0]) {
            const file = this.files[0];
            sendFile(file);
            // 重置输入值
            this.value = '';
        }
    });

    // 图片查看器关闭
    imageModal.addEventListener('click', function() {
        imageModal.classList.remove('show');
    });

    // 阻止点击图片时关闭查看器
    modalImage.addEventListener('click', function(e) {
        e.stopPropagation();
    });

    // 初始化发送按钮状态
    updateSendButton();

    // 点击输入框时确保可见
    messageInput.addEventListener('focus', function() {
        setTimeout(() => {
            scrollToBottom();
        }, 100);
    });

    // 模拟收到消息（演示用）
    setTimeout(() => {
        const reply = createMessageElement('感谢你的消息，已收到！', false, 'text');
        messagesContainer.appendChild(reply);
        scrollToBottom();
    }, 2000);

    // 模拟收到图片消息
    setTimeout(() => {
        const imgSrc = 'https://source.unsplash.com/random/800x600/?nature,123';
        const reply = createMessageElement('看这张美丽的图片！', false, 'image', imgSrc);
        messagesContainer.appendChild(reply);
        scrollToBottom();
    }, 4000);
</script>
</body>
</html>